//
//  ReportSearchCell.m
//  efairy
//
//  Created by 李锋 on 2019/3/20.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ReportSearchCell.h"

@interface ReportSearchCell()<UITextFieldDelegate>

@end
@implementation ReportSearchCell

- (void)awakeFromNib {
    [super awakeFromNib];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}
-(void)initView{
    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, 40)];
    //搜索栏背景
    _searchBg =  [[UIView alloc]init];
    _searchBg.layer.borderWidth=0.0f;
    _searchBg.layer.cornerRadius=16.0f;
    _searchBg.backgroundColor = ColorFromRGB(0xefefef);
    [_searchView addSubview:_searchBg];
    
    //创建左侧搜索图标
    _searchIcon=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    _searchIcon.image=[UIImage imageNamed:@"search_icon"];
    [_searchView addSubview:_searchIcon];
    
    //文本输入框
    _searchText = [[UITextField alloc]init];
    _searchText.backgroundColor = ColorFromRGB(0xefefef);
    _searchText.clearButtonMode=UITextFieldViewModeAlways;//只要输入框有内容就出现
    // 设置提示文字
    _searchText.placeholder = @"请输项目名称";
    _searchText.layer.borderWidth=0.0f;
    _searchText.layer.cornerRadius=16.0f;
    _searchText.returnKeyType = UIReturnKeySearch;//变为搜索按钮
    _searchText.delegate = self;//设置代理
    [_searchView addSubview:_searchText];
    
    
    _searchBg.sd_layout.heightIs(30)
    .topSpaceToView(_searchView, 5)
    .leftSpaceToView(_searchView, 16)
    .rightSpaceToView(_searchView, 16);
    
    _searchIcon.sd_layout.heightIs(15)
    .widthIs(15)
    .topSpaceToView(_searchView, 12.5)
    .leftSpaceToView(_searchView, 32);
    
    _searchText.sd_layout.heightIs(30)
    .topSpaceToView(_searchView, 5)
    .leftSpaceToView(_searchIcon, 12)
    .rightSpaceToView(_searchView, 16);
    
    [self.contentView addSubview:_searchView];
    [self setupAutoHeightWithBottomView:_searchView bottomMargin:0];
    
}

#pragma mark -UITextFieldDelegate
//搜索 这个函数是重写UITextFieldDelegate 当点击键盘搜索按钮触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
     __weak typeof(self) weakSelf = self;
    if(weakSelf.onClickSearchBtn){
        weakSelf.onClickSearchBtn(weakSelf.searchText.text);
    }
    return YES;
}

//颜值搜索栏输入的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.searchText) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            if(self.onChangeTextField){
                self.onChangeTextField(textField.text);
            }
            return YES;
        }
        //so easy
        else if (self.searchText.text.length >= 30) {
            self.searchText.text = [textField.text substringToIndex:30];
            if(self.onChangeTextField){
                self.onChangeTextField(self.searchText.text);
            }
            
            return NO;
        }
    }
    if(self.onChangeTextField){
        self.onChangeTextField(self.searchText.text);
    }
    return YES;
}

-(void)setkeyWord:(NSString *)keyword{
    self.searchText.text = keyword;
}

@end
